﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Senthil.Mobile.WhereAmI
{
    public class TimeChangedEventArgs : EventArgs
    {
        TimeSpan timeToDestination;
        TimeSpan? timeToNextSignificantLocationGroup;

        public TimeSpan TimeToDestination
        {
            get { return timeToDestination; }
        }

        public TimeSpan? TimeToNextSignificantLocationGroup
        {
            get { return timeToNextSignificantLocationGroup; }
        }

        public TimeChangedEventArgs(TimeSpan timeToDestination, TimeSpan? timeToNextSignificantLocationGroup)
        {
            this.timeToDestination = timeToDestination;
            this.timeToNextSignificantLocationGroup = timeToNextSignificantLocationGroup;
        }

        public override bool Equals(object obj)
        {
            if (this == obj)
                return true;

            TimeChangedEventArgs other = obj as TimeChangedEventArgs;

            if (other == null)
                return false;

            return this.timeToDestination == other.timeToDestination && this.timeToNextSignificantLocationGroup == other.timeToNextSignificantLocationGroup;
        }
    }

    public class LocationChangedEventArgs : EventArgs
    {
        Location location;
        Location lastKnownLocation;
        
        LocationGroup nextSignificantLocationGroup;
        IEnumerable<LocationGroup> locationGroupsAfter;

        public LocationGroup NextSignificantLocationGroup
        {
            get { return nextSignificantLocationGroup; }
        }

        public IEnumerable<LocationGroup> LocationGroupsAfter
        {
            get { return locationGroupsAfter; }
        }

        public Location Location
        {
            get { return location; }
        }

        public Location LastKnownLocation
        {
            get { return lastKnownLocation; }
        }

        public LocationChangedEventArgs(Location location, 
            LocationGroup nextSignificantLocationGroup,
            IEnumerable<LocationGroup> locationGroupsAfter,
            Location lastKnownLocation)
        {
            this.location = location;
            this.locationGroupsAfter = locationGroupsAfter;
            this.nextSignificantLocationGroup = nextSignificantLocationGroup;
            this.lastKnownLocation = lastKnownLocation;
        }

        public override bool Equals(object obj)
        {
            if (this == obj)
                return true;

            LocationChangedEventArgs other = obj as LocationChangedEventArgs;

            if (other == null)
                return false;

            bool locationGroupsMatch = false;
            if (this.locationGroupsAfter != null && other.locationGroupsAfter != null)
            {
                List<LocationGroup> l1 = new List<LocationGroup>(locationGroupsAfter);
                List<LocationGroup> l2 = new List<LocationGroup>(other.locationGroupsAfter);
                locationGroupsMatch = ListEquals(l1, l2);
            }
            else
                locationGroupsMatch = other.locationGroupsAfter == null;

            return this.location.Equals(other.location) && 
                this.nextSignificantLocationGroup == other.nextSignificantLocationGroup && 
                locationGroupsMatch &&
                this.lastKnownLocation == other.lastKnownLocation;
        }

        private bool ListEquals<T>(List<T> l1, List<T> l2)
        {
            if (l1.Count != l2.Count)
                return false;

            for (int i = 0; i < l1.Count; ++i)
            {
                if (l1[i] != null)
                {
                    if (!l1[i].Equals(l2[i]))
                        return false;
                }
                else if (l2[i] != null)
                {
                    return false;
                }
                    
            }

            return true;
        }
    }
}
